<?php
namespace Admin\Controller;
use Util\AccessController;
class LoginController extends AccessController {
    public function index(){
    	$this->display();
    }

    public function Handle(){
        $this->S('_REMOVE_');
    	$model['LOGIN'] = $_POST['NAME'];
    	$model['PASSWORD'] = md5($_POST['PASSWORD']);
    	$model['TYPE'] = 1;

    	$ls = Service('Login','login');
        $ps=Service('Personnel','user');
    	$res = $ls->find($model);
        $user = $ps->find("UNID='$res[UNID]'");
    	if($res){
            if($user[LOCK]==1){
                $this->error('您的账号被锁了！');
            }
            $this->S($user);
    		$this->S($res);
    		redirect(U('Index/index'));
    	}else{
    		$this->error('密码或帐号错误');
    	}
    }

    public function wxlogin(){
        
        $model['UNID'] = $_GET[unid];
        $ls = Service('Login','login');
        $ps=Service('Personnel','user');
        $res = $ls->find($model);
        $user = $ps->find("UNID='$res[UNID]'");
        if($res){
            if($user[LOCK]==1){
                $this->error('您的账号被锁了！');
            }
            $this->S($user);
            $this->S($res);
            redirect(U('Index/index'));
        }else{
            $this->error('错误');
        }
    }


    public function updatePwd(){
        $this->display();
    }

    public function update(){
        $ls = Service('Login','login');
        $data['id']=$this->S('UNID');
        $oldpwd=md5($_POST['oldpwd']);
        $newpwd=md5($_POST['newpwd']);
        $secondpwd=md5($_POST['secondpwd']);
        if($newpwd==$secondpwd){
            $data = $ls->findupdate("unid='{$this->S('UNID')}' and password='{$oldpwd}'");
            if($data){
                $up=$ls->updatepwd("UPDATE `t_login` SET `password`='{$newpwd}' WHERE (`UNID`='{$this->S('UNID')}')");
                if($up){
                    $this->success('修改密码成功！', U('Login/updatePwd'));
                }
            }else{
                $this->error('修改密码失败！');
            }
        }else{
            $this->error('两次输入的密码不一样！');
        }
    }

    public function out(){
        $this->S('_REMOVE_');
        redirect(U('index'));
    }

    /**
     * 微信登录。绑定微信号
     * @Author   :ZengYi
     * @DateTime :2017-01-06T09:54:47+0800
     * @Detail   :
     * @return   [type]                    [description]
     */
    public function bind(){   
        $unid = $_GET['UNID'];
        $wechat = new \Util\WechatTicket();
        $wechat->set(C('WX_APPID'), C('WX_SECRET'));
        $wechat->getCode(C('URL_ROOT')."Admin/Login/bindHandle?unid=$unid");
    }

    public function bindHandle(){
        $unid = $_GET['unid'];

        $wechat = new \Util\WechatTicket();
        $wechat->set(C('WX_APPID'), C('WX_SECRET'));
        $userInfo = $wechat->WechatLogin();

        if ($userInfo['errcode'] == 48001) {
            $wechat->getCode(C('URL_ROOT')."Admin/Login/bindHandle", false);
        }
        if(!empty($userInfo['openid'])&&$userInfo['openid']!='C'){
            $ps=Service('Personnel','user');
            $ls = Service('Login','login');
            $data['UNID']=$unid;
            $data['OPENID']=$userInfo['openid'];
            $data['HEAD'] = $userInfo['headimgurl'];
            $data['SEX'] = $userInfo['sex']==1?'男':'女';
            $data['NICKNAME'] = $userInfo['nickname'];
            $Udata=$ps->find("OPENID='{$userInfo['openid']}'");
            $ts=Service('Team','team');
            $Tdata=$ts->find("UNID='{$Udata['TEAM']}'");
            $Ldata=$ls->find("UNID='{$Udata['UNID']}'");
            if(empty($Ldata)){
                if(!empty($Udata)){
                    $ISOK=$ps->updateBind($unid,$Udata,$data['OPENID']);
                }else{
                    $ISOK=$ps->saveInfo("UNID='$unid'",$data);
                }
                if($ISOK){
                    $this->display("success");
                }else{
                    $this->display("error");
                }
            }else if($Ldata['ROLEID']=='583e7d10A01'){
                $ISOK=$ps->saveInfo("UNID='$unid'",$data);
                if($ISOK){
                    $this->display("success");
                }else{
                    $this->display("error");
                }
            }else if($Ldata['ROLEID']!='583e7d10C01'){
                switch ($Ldata['ROLEID']) {
                    case '583e7d10B01':
                        $this->MSG =  '你已经是'.$Tdata['NAME'].'的管理员！';
                        break;
                    case '583e7d10B02':
                        $this->MSG = '你已经是'.$Tdata['NAME'].'的车队客服！';
                        break;
                    case '583e7d10B03':
                        $this->MSG = '你已经是'.$Tdata['NAME'].'的车队司机！';
                        break;
                    default:
                        # code...
                        break;
                }
                $this->display("error");
            }else{
                if(!empty($Udata)){
                    $ISOK=$ps->updateBind($unid,$Udata,$data['OPENID']);
                }else{
                    $ISOK=$ps->saveInfo("UNID='$unid'",$data);
                }
                if($ISOK){
                    $this->display("success");
                }else{
                    $this->display("error");
                }
            }
        }else{
            $this->display("error");
        }
        
    }

    /**
     * 微信登录。绑定微信号(超级管理员)
     * @Author   :ZengYi
     * @DateTime :2017-01-13T09:54:47+0800
     * @Detail   :
     * @return   [type]                    [description]
     */
    public function bindAdmin(){   
        $unid = $_GET['UNID'];
        $wechat = new \Util\WechatTicket();
        $wechat->set(C('WX_APPID'), C('WX_SECRET'));
        $wechat->getCode(C('URL_ROOT')."Admin/Login/bindAdminHandle?unid=$unid");
    }

    public function bindAdminHandle(){
        $unid = $_GET['unid'];

        $wechat = new \Util\WechatTicket();
        $wechat->set(C('WX_APPID'), C('WX_SECRET'));
        $userInfo = $wechat->WechatLogin();

        if ($userInfo['errcode'] == 48001) {
            $wechat->getCode(C('URL_ROOT')."Admin/Login/bindHandle", false);
        }
        if(!empty($userInfo['openid'])&&$userInfo['openid']!='C'){
            $ps=Service('Personnel','user');
            $data['UNID']=$unid;
            $data['WXADMIN']=$userInfo['openid'];
            $ISOK=$ps->saveInfo("UNID='$unid'",$data);
            if($ISOK){
                $this->display("success");
            }else{
                $this->display("error");
            }
        }
        
    }
}

?>
